<!-- 物资弹窗 -->

<template>
  <WebModal path="supply" editTitle="选择物资">
    <MySearch />

    <MyTable />

    <template #footer>
      <BtnPrimary title="选择" :disabled="CORE.isEmpty(supply_param.idList)" @click="toSubmit" />
    </template>
  </WebModal>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'SupplyModal',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [CORE.mixWeb(), CORE.mixWeb('supply', 'supply_'), CORE.mixSelect('point', 'point_')],

  /**
   * 组件导入 (Abc.vue + Abc/index.vue)
   */
  ...CORE.components(require.context('./', true, /vue$/)),

  /**
   * 计算属性 (慎用箭头函数)
   */
  computed: {
    /**
     * 绑定键名
     */
    bind() {
      return this.supply_one.bind
    },
  },

  /**
   * 监听属性 (深度 deep 首次 immediate)
   */
  watch: {
    /**
     * 监听小组弹窗
     */
    supply_visible(visible) {
      visible ? this.init() : this.reset()
    },
  },

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 初始化
     */
    init() {
      const { one, bind, supply_one, supply_param, point_select } = this
      const rowList = one[bind].map((item) => ({ ...item, id: item.supplyId }))
      const idList = rowList.map((row) => row.id)
      const pointId = supply_one.pointId || (point_select[0] || {}).id
      CORE.merge(supply_param, { idList, rowList, pointId })
      this.supply_startList(1)
    },

    /**
     * 重置
     */
    reset() {
      CORE.merge(this.supply_param, { idList: [], rowList: [], pointId: '', name: '' })
    },

    /**
     * 点击选择
     */
    toSubmit() {
      const { one, bind, supply_param } = this

      // 设置选择
      one[bind] = supply_param.rowList.map((item) => ({ ...item, supplyId: item.id }))

      // 关闭弹窗
      this.supply_closeModal()

      // 重置
      this.reset()
    },
  },
}
</script>
